// ignore_for_file: constant_identifier_names

/*
 *  Copyright (C), 2015-2024
 *  FileName: extension_util
 *  Author: Tonight丶相拥
 *  Date: 5/23/24
 *  Description: 
 **/

part of 'flutter_tool.dart';

extension CustomStream on Stream {
  /// 当data 完成时取消订阅
  void dataCompleteThenCancel(void Function(dynamic data)? onData) {
    var subscription = listen(onData);
    subscription.onDone(() {
      /// 数据完成后  取消订阅
      debugLog('subscription done, cancel now');
      subscription.cancel();
    });
  }
}

extension CustomList on List {
  /// 深比较List 是否相等
  bool equalTo(Object other) {
    if (other is List) {
      if (other.length != length) {
        return false;
      }

      for (int i = 0; i < length; i ++) {
        var tempValue = this[i];
        /// 深度比较List
        if (tempValue is List && !tempValue.equalTo(other[i])) {
          return false;
        }
        /// 深度比较Map
        if (tempValue is Map && !tempValue.equalTo(other[i])) {
          return false;
        }
        /// 不是List 、Map 判断是否相等
        if (tempValue is! List && tempValue is! Map && tempValue != other[i]) {
          return false;
        }
      }
      return true;
    }
    return this == other;
  }

  /// 转jsonString
  String toJsonString(){
    try {
      return jsonEncode(this);
    }
    catch(e) {
      return e.toString();
    }
  }

  /// 获取第index 元素
  T? atIndex<T>(int index) {
    if (index >= length) {
      return null;
    }
    return this[index];
  }
}

extension CustomMap on Map {
  /// 判断 Map 是否深度相等
  bool equalTo(Object other) {
    if (other is Map) {
      var keysList = keys.toList();
      if(keysList.length != other.keys.length) {
        return false;
      }
      for (var key in keysList) {
        var value = this[key];
        if (!other.containsKey(key)){
          return false;
        }
        var otherValue = other[key];
        /// 报错  Null 不是 Object 类型 ==  提前判断是否为null
        if (otherValue == null) {
          // return otherValue == value;
          if (value != otherValue) {
            return false;
          }
          continue;
        }
        /// 深度比较
        if (value is List && !value.equalTo(otherValue)) {
          return false;
        }
        /// 深度比较
        if (value is Map && !value.equalTo(otherValue)) {
          return false;
        }
        /// 不是List 并且不是 Map 判断是否不相等
        if (value is! List && value is! Map && value != otherValue) {
          return false;
        }
      }
      return true;
    }
    return this == other;
  }

  /// 转jsonString
  String toJsonString(){
    try {
      return jsonEncode(this);
    }
    catch(e) {
      return e.toString();
    }
  }
}

extension CustomString on String {
  /// 解析成int
  int? tryParseToInt(){
    return int.tryParse(this);
  }

  /// 解析成double
  double? tryParseToDouble(){
    return double.tryParse(this);
  }

  /// 解析成时间
  DateTime? tryParseToDateTime(){
    var time = DateTime.tryParse(this);
    return time;// ?? DateTime(1970,1,1);
  }

  /// 是否有指定匹配
  (bool, List<String?>) hasMatch(RegExp regExpMatch){
    var regResult = regExpMatch.allMatches(this).map((element) => element.group(0)).toList();
    return (regResult.isNotEmpty, regResult);
  }

  /// 转json
  T? toJson<T>() {
    try {
      var result = jsonDecode(this);
      return result as T;
    }catch(e) {
      return null;
    }
  }
}

/*
// ignore: constant_identifier_names
 */
enum CustomDateTimeFormat {
  /// 2002-01-01
  yyyy_mm_dd,
  /// 2002-01-01 09:08:12
  yyyy_mm_dd_hh_mm_ss,
  /// 01-01
  mm_dd,
  /// 2002
  yyyy
}

extension CustomDateTime on DateTime {
  /// 解析成指定格式String
  /// format: 时间格式
  String parseToString({CustomDateTimeFormat format = CustomDateTimeFormat.yyyy_mm_dd_hh_mm_ss}){
    var resultValue = '';
    switch(format) {
      case CustomDateTimeFormat.yyyy_mm_dd:
        resultValue = '$year-${month.defaultFixLength}-${day.defaultFixLength}';
        break;
      case CustomDateTimeFormat.mm_dd:
        resultValue = '${month.defaultFixLength}-${day.defaultFixLength}';
        break;
      case CustomDateTimeFormat.yyyy:
        resultValue = '$year';
        break;
      default:
        resultValue = '$year-${month.defaultFixLength}-${day.defaultFixLength} ${hour.defaultFixLength}:${minute.defaultFixLength}:${second.defaultFixLength}';
        break;
    }
    return resultValue;
  }

  /// 是否与另一个时间相等
  /// time: 另一个时间
  /// format: 时间格式
  bool equalTo(DateTime? time, {CustomDateTimeFormat format = CustomDateTimeFormat.yyyy_mm_dd_hh_mm_ss}){
    var value = false;
    if (time == null) {
      return this == time;
    }
    /// 日期格式控制流转
    switch(format){
      case CustomDateTimeFormat.yyyy_mm_dd_hh_mm_ss:
        value = time.year == year
            && time.month == month
            && time.day == day
            && time.hour == hour
            && time.minute == minute
            && time.second == second;
        break;
      case CustomDateTimeFormat.mm_dd:
        value = time.month == month
            && time.day == day;
        break;
      case CustomDateTimeFormat.yyyy_mm_dd:
        value = time.year == year
            && time.month == month
            && time.day == day;
        break;
      case CustomDateTimeFormat.yyyy:
        value = time.year == year;
        break;
    }
    return value;
  }

  /// 是否是每一年的同一天
  bool isSameDayInPerYear(DateTime? time){
    return equalTo(time, format: CustomDateTimeFormat.mm_dd);
  }

  /// 是否为同一年
  bool isSameYear(DateTime? time) {
    return equalTo(time, format: CustomDateTimeFormat.yyyy);
  }

  /// 是否是同一天
  bool isSameDay(DateTime? time) {
    return equalTo(time, format: CustomDateTimeFormat.yyyy_mm_dd);
  }
}

extension CustomInt on int {
  /// 解析成DateTime
  DateTime tryParseToDateTimeInMilliseconds({bool isUtc = false}){
    var time = DateTime.fromMillisecondsSinceEpoch(this, isUtc: isUtc);
    return time;
  }
  /// 解析成DateTime
  DateTime tryParseToDateTimeInMicroseconds({bool isUtc = false}){
    var time = DateTime.fromMicrosecondsSinceEpoch(this, isUtc: isUtc);
    return time;
  }
  /// 解析成DateTime
  DateTime tryParseToDateTimeInMillisecondsPlus1000({bool isUtc = false}){
    var time = DateTime.fromMillisecondsSinceEpoch(this * 1000, isUtc: isUtc);
    return time;
  }

  /// 位数补足
  String fixLength({int length = 2, String defaultValue = '0'}){
    var value = '$this';
    if (value.length < length) {
      return List.generate(length - value.length, (index) => defaultValue).join() + value;
    }
    return value;
  }

  /// 默认参数补足
  String get defaultFixLength => fixLength();
}

extension CustomNum<T extends num> on T {
  /// 返回更大数
  T max(T other){
    return math.max(other, this);
  }
  /// 返回最小数
  T min(T other) {
    return math.min(other, this);
  }
  /// 是否更大
  bool greaterThan(num value) {
    return this > value;
  }
  /// 是否更小
  bool lessThan(num value) {
    return this < value;
  }
}